{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Animations with Earth Engine\n",
    "**Set the environment**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize Earth Engine account"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ee_satellite_imagery import Composite, Animation\n",
    "from ipywidgets import interact\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from pprint import pprint\n",
    "import getpass\n",
    "import requests\n",
    "import json\n",
    "import os\n",
    "import ee\n",
    "ee.Initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Satellite imagery\n",
    "**Area of Interest**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "geometry = {\n",
    "  \"type\": \"FeatureCollection\",\n",
    "  \"features\": [\n",
    "    {\n",
    "      \"type\": \"Feature\",\n",
    "      \"properties\": {},\n",
    "      \"geometry\": {\n",
    "        \"type\": \"Polygon\",\n",
    "        \"coordinates\": [\n",
    "          [\n",
    "            [\n",
    "              17.60009765625,\n",
    "              -14.744671057048434\n",
    "            ],\n",
    "            [\n",
    "              17.77484893798828,\n",
    "              -14.744671057048434\n",
    "            ],\n",
    "            [\n",
    "              17.77484893798828,\n",
    "              -14.576273583944515\n",
    "            ],\n",
    "            [\n",
    "              17.60009765625,\n",
    "              -14.576273583944515\n",
    "            ],\n",
    "            [\n",
    "              17.60009765625,\n",
    "              -14.744671057048434\n",
    "            ]\n",
    "          ]\n",
    "        ]\n",
    "      }\n",
    "    }\n",
    "  ]\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Static image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%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V0ZWN0UmV0aW5hIjogZmFsc2UsCiAgICAgICAgIm1heE5hdGl2ZVpvb20iOiAxOCwKICAgICAgICAibWF4Wm9vbSI6IDE4LAogICAgICAgICJtaW5ab29tIjogMCwKICAgICAgICAibm9XcmFwIjogZmFsc2UsCiAgICAgICAgIm9wYWNpdHkiOiAxLAogICAgICAgICJzdWJkb21haW5zIjogImFiYyIsCiAgICAgICAgInRtcyI6IGZhbHNlCn0pLmFkZFRvKG1hcF9mZTU1NGVlZWYwYzA0NjkyOGI4NzkyYjMxNjYwNTg5NSk7CiAgICAKICAgICAgICAgICAgICAgIAoKICAgICAgICAgICAgICAgIG1hcF9mZTU1NGVlZWYwYzA0NjkyOGI4NzkyYjMxNjYwNTg5NS5maXRCb3VuZHMoCiAgICAgICAgICAgICAgICAgICAgW1stMTQuNzQ0Njg3NDU1MTg1NzczLCAxNy42MDAwOTc2NTYyNV0sIFstMTQuNTc2MjczNTgzOTQ0NDg4LCAxNy43NzQ4NDg5Mzc5ODgyOF1dLAogICAgICAgICAgICAgICAgICAgIHt9CiAgICAgICAgICAgICAgICAgICAgKTsKICAgICAgICAgICAgCiAgICB2YXIgdGlsZV9sYXllcl83NzVkYWViNGE5MTM0NTE2YmE3MDdlMTRmMWUyYjA0NSA9IEwudGlsZUxheWVyKAogICAgICAgICdodHRwczovL2VhcnRoZW5naW5lLmdvb2dsZWFwaXMuY29tL21hcC82MWQyYzk0OGE4ZWY3NzBhNGRmMWU0ZGRlOTk4MjY4NC97en0ve3h9L3t5fT90b2tlbj0wZDAzODk0M2VhZGM3MmE4OWI3NmVmNDY1NmRmMDllZScsCiAgICAgICAgewogICAgICAgICJhdHRyaWJ1dGlvbiI6ICJHb29nbGUgRWFydGggRW5naW5lIiwKICAgICAgICAiZGV0ZWN0UmV0aW5hIjogZmFsc2UsCiAgICAgICAgIm1heE5hdGl2ZVpvb20iOiAxOCwKICAgICAgICAibWF4Wm9vbSI6IDE4LAogICAgICAgICJtaW5ab29tIjogMCwKICAg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0dXJuIGZlYXR1cmUucHJvcGVydGllcy5zdHlsZTt9KTsKICAgICAgICAKICAgIAogICAgICAgICAgICB2YXIgbGF5ZXJfY29udHJvbF85ODQ1MGZkZjAyZDk0OWE0YjJmZTAzMDQ2YzFkM2IzOSA9IHsKICAgICAgICAgICAgICAgIGJhc2VfbGF5ZXJzIDogeyAib3BlbnN0cmVldG1hcCIgOiB0aWxlX2xheWVyX2IzNGZmZWYwMjAxZDRjYWViMGRjOTE5NzQzMmEyMTZlLCB9LAogICAgICAgICAgICAgICAgb3ZlcmxheXMgOiB7ICJMYW5kc2F0OCIgOiB0aWxlX2xheWVyXzc3NWRhZWI0YTkxMzQ1MTZiYTcwN2UxNGYxZTJiMDQ1LCJBb0kiIDogZ2VvX2pzb25fNjk5ODMxYzljYWZmNGVkOWE1YmM4ODI1NWE4NzIyMDcsIH0KICAgICAgICAgICAgICAgIH07CiAgICAgICAgICAgIEwuY29udHJvbC5sYXllcnMoCiAgICAgICAgICAgICAgICBsYXllcl9jb250cm9sXzk4NDUwZmRmMDJkOTQ5YTRiMmZlMDMwNDZjMWQzYjM5LmJhc2VfbGF5ZXJzLAogICAgICAgICAgICAgICAgbGF5ZXJfY29udHJvbF85ODQ1MGZkZjAyZDk0OWE0YjJmZTAzMDQ2YzFkM2IzOS5vdmVybGF5cywKICAgICAgICAgICAgICAgIHtwb3NpdGlvbjogJ3RvcHJpZ2h0JywKICAgICAgICAgICAgICAgICBjb2xsYXBzZWQ6IHRydWUsCiAgICAgICAgICAgICAgICAgYXV0b1pJbmRleDogdHJ1ZQogICAgICAgICAgICAgICAgfSkuYWRkVG8obWFwX2ZlNTU0ZWVlZjBjMDQ2OTI4Yjg3OTJiMzE2NjA1ODk1KTsKICAgICAgICAgICAgCiAgICAgICAgCjwvc2NyaXB0Pg==\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
      ],
      "text/plain": [
       "<folium.folium.Map at 0x114698130>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "composite = Composite(geometry=geometry, startDate = ee.Date('2019-01-01'), stopDate = ee.Date('2019-12-31'), collection = 'Landsat8')\n",
    "composite.read_composite()\n",
    "composite.display_composite_folium()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Save image as `png`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "composite.save_composite_as_png('../data/', 'Sentinel2_6')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Time-lapse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "animation = Animation(geometry=geometry, start_year = 2010, stop_year = 2019, instrument = 'Landsat')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Numpy array**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Image number: 0\n",
      "Image number: 1\n",
      "Image number: 2\n",
      "Image number: 3\n",
      "Image number: 4\n",
      "Image number: 5\n",
      "Image number: 6\n",
      "Image number: 7\n",
      "Image number: 8\n",
      "Image number: 9\n"
     ]
    }
   ],
   "source": [
    "video = animation.video_as_array(dimensions=1024)\n",
    "video = animation.video_add_elements(logo_path='../data/mongabay-horizontal.jpg', y_pixels=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "11dc5c64511949309f5b42b4d80d960f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(IntSlider(value=0, description='frame', max=9), Output()), _dom_classes=('widget-interac…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "@interact(frame=(0, video.shape[0]-1))\n",
    "def show_frame(frame=0):\n",
    "    plt.figure(figsize=(20,10))\n",
    "    plt.imshow(video[frame,:,:,:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Create animations from a Numpy Array**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "animation.create_movie_from_array('../data/ladsat8.mp4', output_format='mp4')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Upload animation to Google cloud storage**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source_file_name = '../data/movie.mp4'\n",
    "destination_blob_name = 'movie-tiles/mongabay/movie.mp4'\n",
    "animation.upload_blob(source_file_name, destination_blob_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Remove file from local directory**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.remove('../data/movie.mp4')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Display animation on map**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9420186f787941d1af5e051d762ccfdf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map(center=[-14.660477843347442, 17.687473297119272], controls=(ZoomControl(options=['position', 'zoom_in_text…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "url = \"https://storage.cloud.google.com/skydipper_materials/movie-tiles/mongabay/movie.mp4\"\n",
    "animation.display_animation(url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Deploying Cloud Functions\n",
    "\n",
    "To create a Google Cloud Function we need a [Google Cloud Project](https://cloud.google.com/resource-manager/docs/creating-managing-projects) and [gcloud SDK](https://cloud.google.com/sdk/docs/).\n",
    "\n",
    "If we have already some projects we can check them by typing:\n",
    "\n",
    "`gcloud projects list`\n",
    "```\n",
    "PROJECT_ID        NAME                     PROJECT_NUMBER\n",
    "gef-ld-toolbox    gef-ld-toolbox           1080184168142\n",
    "gfw-apis          Global Forest Watch API  872868960419\n",
    "resource-watch    Resource Watch           312603932249\n",
    "skydipper-196010  skydipper                230510979472\n",
    "soc-platform      SOC Platform             345072612231\n",
    "```\n",
    "and select one by:\n",
    "\n",
    "`gcloud config set project skydipper-196010`\n",
    "\n",
    "Then create a function by creating a `main.py` file with some python code on it:\n",
    "\n",
    "```python\n",
    "from ee_satellite_imagery import Animation\n",
    "import json\n",
    "import ee\n",
    "\n",
    "service_account = 'skydipper@skydipper-196010.iam.gserviceaccount.com'\n",
    "credentials = ee.ServiceAccountCredentials(service_account, 'privatekey.json')\n",
    "ee.Initialize(credentials)\n",
    "\n",
    "def serializer(url):\n",
    "\n",
    "    return {\n",
    "        'download_url': url\n",
    "    }\n",
    "\n",
    "def animation(request):\n",
    "    request = request.get_json()\n",
    "    \n",
    "    animation = Animation(geometry=request['geometry'], start_year = request['start_year'], stop_year = request['stop_year'], instrument = request['instrument'])\n",
    "    \n",
    "    # Numpy array\n",
    "    video = animation.video_as_array(dimensions=request['dimensions'])\n",
    "\n",
    "    # Create animations from a Numpy Array\n",
    "    animation.create_movie_from_array('movie.mp4', output_format='mp4')\n",
    "    \n",
    "    # Upload animation to Google cloud storage\n",
    "    print('Uploadong animation to Google cloud storage')\n",
    "    source_file_name = 'movie.mp4'\n",
    "    destination_blob_name = 'movie-tiles/mongabay/movie.mp4'\n",
    "    animation.upload_blob(source_file_name, destination_blob_name)\n",
    "\n",
    "    url = \"https://storage.cloud.google.com/skydipper_materials/movie-tiles/mongabay/movie.mp4\"\n",
    "\n",
    "    return json.dumps(serialize_response(url))\n",
    "```\n",
    "\n",
    "In the same directory include the `privatekey.json` with the [service account keys](https://cloud.google.com/iam/docs/creating-managing-service-account-keys) and the `requirements.txt` file. \n",
    "\n",
    "Finally cd to that directory and deploy the cloud Function with the following command:\n",
    "\n",
    "`gcloud beta functions deploy animation --runtime python37 --trigger-http --timeout 540` \n",
    "\n",
    "Note that the cloud function name matches the name of the function we defined in code: `animation`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Call the Cloud Functions\n",
    "\n",
    "You can invoke Cloud Functions with an HTTP request using the POST, PUT, GET, DELETE, and OPTIONS HTTP methods. To find the `URL` that triggers the function open the details of the created function and click on the `Trigger` tab."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get your Google OAuth token with the following command:\n",
    "\n",
    "`gcloud auth print-identity-token`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Provide a Google OAuth token here: ······························································································································································································································································································································································································································································································································································································································································································································································································································································································\n"
     ]
    }
   ],
   "source": [
    "token = getpass.getpass('Provide a Google OAuth token here:')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload =   {\n",
    "    \"start_year\": 2010,\n",
    "    \"stop_year\": 2019,\n",
    "    \"instrument\": \"Landsat\",\n",
    "    \"dimensions\":512,\n",
    "    \"geometry\": {\n",
    "  \"type\": \"FeatureCollection\",\n",
    "  \"features\": [\n",
    "    {\n",
    "      \"type\": \"Feature\",\n",
    "      \"properties\": {},\n",
    "      \"geometry\": {\n",
    "        \"type\": \"Polygon\",\n",
    "        \"coordinates\": [\n",
    "          [\n",
    "            [\n",
    "              17.60009765625,\n",
    "              -14.744671057048434\n",
    "            ],\n",
    "            [\n",
    "              17.77484893798828,\n",
    "              -14.744671057048434\n",
    "            ],\n",
    "            [\n",
    "              17.77484893798828,\n",
    "              -14.576273583944515\n",
    "            ],\n",
    "            [\n",
    "              17.60009765625,\n",
    "              -14.576273583944515\n",
    "            ],\n",
    "            [\n",
    "              17.60009765625,\n",
    "              -14.744671057048434\n",
    "            ]\n",
    "          ]\n",
    "        ]\n",
    "      }\n",
    "    }\n",
    "  ]\n",
    "}\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'download_url': 'https://storage.cloud.google.com/skydipper_materials/movie-tiles/mongabay/movie.mp4'}\n",
      "CPU times: user 37.3 ms, sys: 13.2 ms, total: 50.5 ms\n",
      "Wall time: 1min 41s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "url = f'https://us-central1-skydipper-196010.cloudfunctions.net/animation'\n",
    "\n",
    "headers = {'Authorization': 'Bearer ' + token, 'Content-Type': 'application/json'}\n",
    "#headers = {'Content-Type': 'application/json'}\n",
    "\n",
    "r = requests.post(url, data=json.dumps(payload), headers=headers)\n",
    "pprint(r.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
